package com.ruoyi.web.controller.activity;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TActivityInfo;
import com.ruoyi.system.service.ITActivityInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 活动信息Controller
 *
 * @author xiyu
 * @date 2024-12-01
 */
@RestController
@RequestMapping("/activity/info")
public class TActivityInfoController extends BaseController {
    @Autowired
    private ITActivityInfoService tActivityInfoService;

    /**
     * 查询活动信息列表
     */
    @PreAuthorize("@ss.hasPermi('activity:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(TActivityInfo tActivityInfo) {
        startPage();
        List<TActivityInfo> list = tActivityInfoService.selectTActivityInfoList(tActivityInfo);
        return getDataTable(list);
    }

    /**
     * 导出活动信息列表
     */
    @PreAuthorize("@ss.hasPermi('activity:info:export')")
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TActivityInfo tActivityInfo) {
        List<TActivityInfo> list = tActivityInfoService.selectTActivityInfoList(tActivityInfo);
        ExcelUtil<TActivityInfo> util = new ExcelUtil<TActivityInfo>(TActivityInfo.class);
        util.exportExcel(response, list, "活动信息数据");
    }

    /**
     * 获取活动信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tActivityInfoService.selectTActivityInfoById(id));
    }

    /**
     * 新增活动信息
     */
    @PreAuthorize("@ss.hasPermi('activity:info:add')")
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TActivityInfo tActivityInfo) {
        tActivityInfo.setCreateBy(getUsername());
        return toAjax(tActivityInfoService.insertTActivityInfo(tActivityInfo));
    }

    /**
     * 修改活动信息
     */
    @PreAuthorize("@ss.hasPermi('activity:info:edit')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TActivityInfo tActivityInfo) {
        tActivityInfo.setUpdateBy(getUsername());
        return toAjax(tActivityInfoService.updateTActivityInfo(tActivityInfo));
    }

    /**
     * 删除活动信息
     */
    @PreAuthorize("@ss.hasPermi('activity:info:remove')")
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids, @PathVariable String actStatus) {
        return toAjax(tActivityInfoService.deleteTActivityInfoByIds(ids, actStatus));
    }

    /**
     * 获取有效的活动列表
     */
    @GetMapping("/getValidActInfoList")
    public AjaxResult getValidActInfoList(TActivityInfo tActivityInfo) {
        if (StringUtils.isEmpty(tActivityInfo.getActStatus())) {
            tActivityInfo.setActStatus("1");
        }
        return success(tActivityInfoService.getActivityNoOrNameList(tActivityInfo));
    }
}
